"use cilent";
import { updateQuestionUsingPost } from "@/api/questionController";
import { ProColumns, ProTable } from "@ant-design/pro-components";
import { message, Modal } from "antd";
import { on } from "events";
import React from "react";

interface Props {
  isModalOpen: boolean;
  columns?: ProColumns<API.Question>[];
  oldData: API.Question;
  onSubmit?: (values: API.QuestionAddRequest) => void;
  onCancel?: () => void;
}
/**
 * 修改节点
 * @returns
 */
const handleUpdate = async (values: API.QuestionAddRequest) => {
  try {
    await updateQuestionUsingPost(values);
    return true;
  } catch (error) {
    return false;
  }
};
/**
 * 修改弹窗
 * @returns
 */
const UpdateModal: React.FC<Props> = (props: Props) => {
  const { isModalOpen, columns, oldData, onSubmit, onCancel } = props;
  const [messageApi, contextHolder] = message.useMessage();
  const initValues = { ...oldData };
  if (oldData.tags) {
    // initValues.tags = JSON.parse(oldData.tags) || [];
  }
  return (
    <>
      {contextHolder}
      <Modal
        title="修改用户"
        open={isModalOpen}
        onCancel={() => onCancel?.()}
        centered={true}
        footer={null}
      >
        <ProTable
          type="form"
          form={{ initialValues: initValues }}
          columns={columns}
          onSubmit={async (values: API.QuestionAddRequest) => {
            messageApi.open({
              content: "更新中...",
            });
            const formData = { ...values, id: oldData.id };
            await updateQuestionUsingPost(formData);
            onSubmit?.(formData);
            messageApi.destroy();
            messageApi.success("更新成功");
          }}
        />
      </Modal>
    </>
  );
};
export default UpdateModal;
